package com.yanqu.road.server.cmd.activity.dragonboatrace;

import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.dragonboatrace.DragonBoatRaceProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.dragonboatrace.DragonBoatRaceModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.activity.dragonboatrace.DragonBoatRaceMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Cmd(code = GameProtocol.S_DRAGONBOATRACE_START, desc = "赛龙舟开始游戏")
public class DragonBoatRaceStartCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DragonBoatRaceProto.DragonBoatRaceStartReqMsg reqMsg = DragonBoatRaceProto.DragonBoatRaceStartReqMsg.parseFrom(packet.getBytes());
        if(reqMsg.getPatronsIdCount() != DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_PATRONS_NUM()){
            ret = GameErrorCode.E_DRAGONBOATRACE_ACTIVITY_PATRONS_NUM_IS_WRONG;
            return;
        }
        DragonBoatRaceModule dragonBoatRaceModule = DragonBoatRaceMgr.getDragonBoatRaceModule(player);
        ret = dragonBoatRaceModule.canStartGame();
        if(ret != 0){
            return;
        }
        int addSpeed = 0;
        PatronsModule patronsModule = player.getModule(PatronsModule.class);
        // 判断门客是否可派遣  判断传入的门客id不能重复
        List<Integer> tempList = new ArrayList<>();
        for(Integer patronsId : reqMsg.getPatronsIdList()){
            UserPatrons userPatrons = patronsModule.getUserPatrons(patronsId);
            if(userPatrons == null){
                ret = GameErrorCode.E_DRAGONBOATRACE_ACTIVITY_PATRONS_NOT_EXIST;
                return;
            }
            if(dragonBoatRaceModule.dragonBoatRaceUser.baseData.getPatronsList().contains(patronsId)){
                ret = GameErrorCode.E_DRAGONBOATRACE_DISPATCH_PATRONS_REPEATED;
                return;
            }
            if(tempList.contains(patronsId)){
                ret = GameErrorCode.E_DRAGONBOATRACE_PATRONS_ID_IS_DUPLICATE;
                return;
            }
            tempList.add(patronsId);
            double totalAbility = userPatrons.getAbility().doubleValue();
            addSpeed += (int)(Math.ceil(Math.pow(totalAbility,DragonBoatRaceMgr.getConfig().getPatronsLevelCoefficient().get(0)/1000d) * DragonBoatRaceMgr.getConfig().getPatronsLevelCoefficient().get(1) / 1000d));
        }
        // 需要重置比赛  有可能中途退出
        dragonBoatRaceModule.clearGame();

        long startTime = System.currentTimeMillis() + DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_PREPARATION_TIME() * DateHelper.SECOND_MILLIONS;
        dragonBoatRaceModule.dragonBoatRaceUser.startTime = startTime;
        dragonBoatRaceModule.dragonBoatRaceUser.endTime = startTime + DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_GAME_TIME() * DateHelper.SECOND_MILLIONS;
        dragonBoatRaceModule.dragonBoatRaceUser.initSpeed = DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_INIT_SPEED() + addSpeed;
        dragonBoatRaceModule.dragonBoatRaceUser.curSpeed = dragonBoatRaceModule.dragonBoatRaceUser.initSpeed;
        dragonBoatRaceModule.dragonBoatRaceUser.curCalTime = startTime;
        dragonBoatRaceModule.dragonBoatRaceUser.patronsList.addAll(reqMsg.getPatronsIdList());

        Random random = new Random(startTime);
        long tempTime = startTime;
        while (true){
            int meetTime = random.nextInt(DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_RANDOM_END_RANGE()-DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_RANDOM_START_RANGE())+DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_RANDOM_START_RANGE();
            tempTime += meetTime*DateHelper.SECOND_MILLIONS;
            if(tempTime < dragonBoatRaceModule.dragonBoatRaceUser.endTime){
                dragonBoatRaceModule.dragonBoatRaceUser.produceObstacleTimeList.add(tempTime);
                dragonBoatRaceModule.createNodeStatus(1,0,tempTime,0,false);
            }else {
                break;
            }
        }
        DragonBoatRaceProto.DragonBoatRaceStartRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceStartRespMsg.newBuilder();
        builder.setRet(0);
        builder.setInitSpeed(dragonBoatRaceModule.dragonBoatRaceUser.initSpeed);
        builder.setStartTime(dragonBoatRaceModule.dragonBoatRaceUser.startTime);
        builder.addAllProduceObstacleTimeList(dragonBoatRaceModule.dragonBoatRaceUser.produceObstacleTimeList);
        player.sendPacket(ClientProtocol.U_DRAGONBOATRACE_START, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DragonBoatRaceProto.DragonBoatRaceStartRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceStartRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_DRAGONBOATRACE_START, builder);
        }
    }
}
